﻿<?php
//require_once 'acc_config.php'; //require时,是按照当前文件的路径去查找相对路径的
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/init/acc_tab_builder.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/acc_conn.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/rpc_com.php';

function check_mysql_conn($svr, $usr, $pwd) {
	$pdb = new acc_DB();
	print "连接mysql成功!";
}

function dorpc_check_mysql_conn($data, &$res) {
    $db_path = $_SERVER['DOCUMENT_ROOT'] . '/accHestia/db/acc.db';
    if(file_exists($db_path)) {
        if (!unlink($db_path)) {
            set_res_NG($res, "delete file failed: $db_path");
            return;
        }
    }
    $db_path_init = $_SERVER['DOCUMENT_ROOT'] . '/accHestia/db/acc_init.db';
    if(!copy($db_path_init, $db_path)) { //copy成功则返回 TRUE
        set_res_NG($res, "copy failed:$db_path_init -> $db_path");
        return;
    }
        
    $db = new acc_DB();
    if(!$db){
        set_res_NG($res, $db->lastErrorMsg());
    } else {
        set_res_OK($res, "db:" . var_export($db, true));
    }
}

function dorpc_rebuild_acc_db($data, &$res) {
	// if (!unlink($_SERVER['DOCUMENT_ROOT'] . '/accHestia/db/acc.db')) {
		// echo ("Error deleting db.");
		// return;
	// }

	// $db = new acc_DB();
	set_res_OK($res);
}

function dorpc_set_admin_pwd($data, &$res) {
	$pdb = new acc_DB();
    $nameSql = "";
    if(isset($data["admName"]) && "" != $data["admName"]) {
        $nameSql = "uName='" . $data["admName"] . "', ";
    }
    
	$ssql = "UPDATE users SET pwd='" . $data["admPwd"] . "', " .
			                 $nameSql .
                             "email='" . $data["admEmail"] . "' WHERE loginId='admin';";
	if (!$pdb->query($ssql)) {
        return set_res_NG($res, "update admin failed:" . $$ssql, "update admin user failed.");
	}
    set_res_OK($res);
}

function dorpc_set_acc_category($data, &$res) {
	buildacc_by_category($data["cate"], $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/init/acc.xml');
    $db = new acc_DB();
    $ssql = "update acc_confs set value=1 where item='is_intial_done'";
	if (!$db->query($ssql)) {
        set_res_NG($res, $ssql);
        return;
    }
	set_res_OK($res, $ssql);
}

?>